<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "bid_rcd".
 *
 * @property integer $bidrcdid
 * @property integer $product_id
 * @property integer $auction_id
 * @property integer $user_id
 * @property string $img
 * @property string $name
 * @property string $province
 * @property string $city
 * @property string $county
 * @property integer $state
 * @property double $price
 * @property integer $created_at
 * @property integer $updated_at
 * @property User $user
 */
class BidRcd extends ActiveRecord
{

    const STATE_LEAD = 1;
    const STATE_OUT = 0;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'state'=>[
                self::STATE_LEAD=>'领先',
                self::STATE_OUT=>'出局',
            ],
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bid_rcd';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product_id', 'auction_id', 'user_id', 'img', 'name', 'province', 'city', 'county', 'state', 'price'], 'required'],
            [['product_id', 'auction_id', 'user_id', 'state'], 'integer'],
            [['price'], 'number'],
            [['img', 'name', 'province', 'city', 'county'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'bidrcdid' => 'ID',
            'product_id' => '产品id',
            'auction_id' => '竞拍id',
            'user_id' => '用户id',
            'img' => '头像',
            'name' => '用户昵称',
            'province' => '省',
            'city' => '市',
            'county' => '区',
            'state' => '状态',
            'price' => '成交价',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public static function create(BidQueue $bidqueue,$price)
    {
        $model = new self();
        $model->product_id = $bidqueue->product_id;
        $model->auction_id = $bidqueue->auction_id;
        $model->user_id = $bidqueue->user_id;
        $model->img = $bidqueue->img;
        $model->name = $bidqueue->name;
        $model->province = $bidqueue->province;
        $model->city = $bidqueue->city;
        $model->price = $price;
        $model->state = self::STATE_LEAD;
        $model->save(false);
        $bidqueue->delete();

        return $model;
    }

    public function getUser()
    {
        return $this->hasOne(User::className(),['userid'=>'user_id']);
    }

    public function getAuction()
    {
        return $this->hasOne(Auction::className(),['auctionid'=>'auction_id']);
    }

    public function getProduct()
    {
        return $this->hasOne(Product::className(),['pid'=>'product_id']);
    }
}
